/*
 * @(#)DmsVersion.java
 *
 * Copyright (c) 2003 DCIVision Ltd
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of DCIVision
 * Ltd ("Confidential Information").  You shall not disclose such Confidential
 * Information and shall use it only in accordance with the terms of the license
 * agreement you entered into with DCIVision Ltd.
 */
package com.dcivision.dms.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import com.dcivision.dms.bean.DmsVersion;
import com.dcivision.dms.web.ListDmsVersionForm;
import com.dcivision.framework.ApplicationException;
import com.dcivision.framework.ErrorConstant;
import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.SessionContainer;
import com.dcivision.framework.TextUtility;
import com.dcivision.framework.UserInfoFactory;
import com.dcivision.framework.Utility;
import com.dcivision.framework.bean.AbstractBaseObject;
import com.dcivision.framework.dao.AbstractDAObject;
import com.dcivision.framework.web.AbstractSearchForm;
import com.dcivision.workflow.bean.WorkflowProgressAttachment;

/**
  DmsVersionDAObject.java

  This class is the data access bean for table "DMS_VERSION".

  @author      Zoe Shum
  @company     DCIVision Limited
  @creation date   05/08/2003
  @version     $Revision: 1.29.4.3 $
*/

public class DmsVersionDAObject extends AbstractDAObject {

  public static final String REVISION = "$Revision: 1.29.4.3 $";

  public static final String TABLE_NAME = "DMS_VERSION";

  public DmsVersionDAObject(SessionContainer sessionContainer, Connection dbConn) {
    super(sessionContainer, dbConn);
  }

  
  protected void initDBSetting() {
    this.baseTableName = TABLE_NAME;
    this.vecDBColumn.add("ID");
    this.vecDBColumn.add("DOCUMENT_ID");
    this.vecDBColumn.add("VERSION_NUMBER");
    this.vecDBColumn.add("VERSION_LABEL");
    this.vecDBColumn.add("PARENT_ID");
    this.vecDBColumn.add("CONTENT_ID");
    this.vecDBColumn.add("BK_ARCHIVE_ID");
    this.vecDBColumn.add("REFERENCE_NO");
    this.vecDBColumn.add("DESCRIPTION");
    this.vecDBColumn.add("ITEM_SIZE");
    this.vecDBColumn.add("ITEM_STATUS");
    this.vecDBColumn.add("RESTORE_STATUS");
    this.vecDBColumn.add("RECORD_STATUS");
    this.vecDBColumn.add("UPDATE_COUNT");
    this.vecDBColumn.add("CREATOR_ID");
    this.vecDBColumn.add("CREATE_DATE");
    this.vecDBColumn.add("UPDATER_ID");
    this.vecDBColumn.add("UPDATE_DATE");
  }

  
  protected synchronized AbstractBaseObject getByID(Integer id) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_NUMBER, A.VERSION_LABEL, A.PARENT_ID, A.CONTENT_ID, A.BK_ARCHIVE_ID, A.REFERENCE_NO, A.DESCRIPTION, A.ITEM_SIZE, A.ITEM_STATUS, A.RESTORE_STATUS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_VERSION A ");
        sqlStat.append("WHERE  A.ID = ? AND A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, id);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        if (rs.next()) {
          DmsVersion tmpDmsVersion = new DmsVersion();
          tmpDmsVersion.setID(getResultSetInteger(rs, "ID"));
          tmpDmsVersion.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsVersion.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpDmsVersion.setVersionLabel(getResultSetString(rs, "VERSION_LABEL"));
          tmpDmsVersion.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpDmsVersion.setContentID(getResultSetInteger(rs, "CONTENT_ID"));
          tmpDmsVersion.setBkArchiveID(getResultSetInteger(rs, "BK_ARCHIVE_ID"));
          tmpDmsVersion.setReferenceNo(getResultSetString(rs, "REFERENCE_NO"));
          tmpDmsVersion.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsVersion.setItemSize(getResultSetInteger(rs, "ITEM_SIZE"));
          tmpDmsVersion.setItemStatus(getResultSetString(rs, "ITEM_STATUS"));
          tmpDmsVersion.setRestoreStatus(getResultSetString(rs, "RESTORE_STATUS"));
          tmpDmsVersion.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsVersion.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsVersion.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsVersion.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsVersion.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsVersion.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsVersion.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsVersion.getCreatorID()));
          tmpDmsVersion.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsVersion.getUpdaterID()));
          return(tmpDmsVersion);
        } else {
          throw new ApplicationException(ErrorConstant.DB_RECORD_NOT_FOUND_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized List getList(AbstractSearchForm searchForm) throws ApplicationException {
    PreparedStatement preStat = null;
    PreparedStatement preStatCnt = null;
    ResultSet rs = null;
    ResultSet rsCnt = null;
    StringBuffer sqlStat = new StringBuffer();
    StringBuffer sqlStatCnt = new StringBuffer();
    List result = new ArrayList();
    int totalNumOfRecord = 0;
    int rowLoopCnt = 0;
    int startOffset = TextUtility.parseInteger(searchForm.getCurStartRowNo());
    int pageSize = TextUtility.parseInteger(searchForm.getPageOffset());
    ListDmsVersionForm verSearchForm = (ListDmsVersionForm)searchForm;
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_NUMBER, A.VERSION_LABEL, A.PARENT_ID, A.CONTENT_ID, A.BK_ARCHIVE_ID, A.REFERENCE_NO, A.DESCRIPTION, A.ITEM_SIZE, A.ITEM_STATUS, A.RESTORE_STATUS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_VERSION A ");
        sqlStat.append("WHERE A.RECORD_STATUS = ? AND A.DOCUMENT_ID = ?   ");

        // Form the WHERE clause for filtering.
        /*
        if (searchForm.isSearchable()) {
          String searchField = searchForm.getBasicSearchField();
          if (searchField.indexOf(".") < 0) {
            searchField = "A." + searchField;
          }
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }
        */
        
        if (searchForm.isSearchable()) {
          String searchField = getSearchColumn(searchForm.getBasicSearchField());
          sqlStat.append("AND  " + searchField + " " + searchForm.getBasicSearchType() + " ? ");
        }
        

        //format the sql for any 'LIKE' statement contained
        sqlStat = this.getFormattedSQL(sqlStat.toString());

        // Form the ORDER clause for sorting.
        //sqlStat.append("ORDER BY A.ID, A.PARENT_ID, A.VERSION_NUMBER ");
        if (searchForm.isSortable()) {
          String sortAttribute = searchForm.getSortAttribute();
          if (sortAttribute.indexOf(".") < 0) {
            sortAttribute = "A." + sortAttribute;
          }
          sqlStat.append("ORDER BY " + sortAttribute + " " + searchForm.getSortOrder());
        }

        // Get total number of record return.
        sqlStatCnt = this.getSelectCountSQL(sqlStat);
        preStatCnt = dbConn.prepareStatement(sqlStatCnt.toString());
        this.setPrepareStatement(preStatCnt, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStatCnt, 2, verSearchForm.getDocumentID());
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStatCnt, 3, searchKeyword);
        }
        rsCnt = preStatCnt.executeQuery();
        if (rsCnt.next()) {
          totalNumOfRecord = rsCnt.getInt(1);
        }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }

        // Retrieve the result in row basis.
        sqlStat = this.getSelectListSQL(sqlStat, startOffset, pageSize);
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 2, verSearchForm.getDocumentID());
        if (searchForm.isSearchable()) {
          String searchKeyword = this.getFormattedKeyword(searchForm.getBasicSearchKeyword(), searchForm.getBasicSearchType());
          this.setPrepareStatement(preStat, 3, searchKeyword);
        }
        rs = preStat.executeQuery();

        this.positionCursor(rs, startOffset, pageSize);

        while (rs.next() && rowLoopCnt<pageSize) {
          DmsVersion tmpDmsVersion = new DmsVersion();
          tmpDmsVersion.setID(getResultSetInteger(rs, "ID"));
          tmpDmsVersion.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsVersion.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpDmsVersion.setVersionLabel(getResultSetString(rs, "VERSION_LABEL"));
          tmpDmsVersion.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpDmsVersion.setContentID(getResultSetInteger(rs, "CONTENT_ID"));
          tmpDmsVersion.setBkArchiveID(getResultSetInteger(rs, "BK_ARCHIVE_ID"));
          tmpDmsVersion.setReferenceNo(getResultSetString(rs, "REFERENCE_NO"));
          tmpDmsVersion.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsVersion.setItemSize(getResultSetInteger(rs, "ITEM_SIZE"));
          tmpDmsVersion.setItemStatus(getResultSetString(rs, "ITEM_STATUS"));
          tmpDmsVersion.setRestoreStatus(getResultSetString(rs, "RESTORE_STATUS"));
          tmpDmsVersion.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsVersion.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsVersion.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsVersion.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsVersion.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsVersion.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsVersion.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsVersion.getCreatorID()));
          tmpDmsVersion.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsVersion.getUpdaterID()));
          tmpDmsVersion.setRecordCount(totalNumOfRecord);
          tmpDmsVersion.setRowNum(startOffset++);
          ++rowLoopCnt;
          result.add(tmpDmsVersion);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        try { rsCnt.close(); } catch (Exception ignore) {} finally { rsCnt = null; }
        try { preStatCnt.close(); } catch (Exception ignore) {} finally { preStatCnt = null; }
      }
    }
  }

  
  protected synchronized List getList() throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_NUMBER, A.VERSION_LABEL, A.PARENT_ID, A.CONTENT_ID, A.BK_ARCHIVE_ID, A.REFERENCE_NO, A.DESCRIPTION, A.ITEM_SIZE, A.ITEM_STATUS, A.RESTORE_STATUS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_VERSION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsVersion tmpDmsVersion = new DmsVersion();
          tmpDmsVersion.setID(getResultSetInteger(rs, "ID"));
          tmpDmsVersion.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsVersion.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpDmsVersion.setVersionLabel(getResultSetString(rs, "VERSION_LABEL"));
          tmpDmsVersion.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpDmsVersion.setContentID(getResultSetInteger(rs, "CONTENT_ID"));
          tmpDmsVersion.setBkArchiveID(getResultSetInteger(rs, "BK_ARCHIVE_ID"));
          tmpDmsVersion.setReferenceNo(getResultSetString(rs, "REFERENCE_NO"));
          tmpDmsVersion.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsVersion.setItemSize(getResultSetInteger(rs, "ITEM_SIZE"));
          tmpDmsVersion.setItemStatus(getResultSetString(rs, "ITEM_STATUS"));
          tmpDmsVersion.setRestoreStatus(getResultSetString(rs, "RESTORE_STATUS"));
          tmpDmsVersion.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsVersion.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsVersion.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsVersion.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsVersion.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsVersion.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsVersion.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsVersion.getCreatorID()));
          tmpDmsVersion.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsVersion.getUpdaterID()));
          result.add(tmpDmsVersion);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected void validateInsert(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateUpdate(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected void validateDelete(AbstractBaseObject obj) throws ApplicationException {
  }

  
  protected synchronized AbstractBaseObject insert(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsVersion tmpDmsVersion = (DmsVersion)((DmsVersion)obj).clone();

    synchronized(dbConn) {
      try {
        Integer nextID = getNextPrimaryID();
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("INSERT ");
        sqlStat.append("INTO   DMS_VERSION(ID, DOCUMENT_ID, VERSION_NUMBER, VERSION_LABEL, PARENT_ID, CONTENT_ID, BK_ARCHIVE_ID, REFERENCE_NO, DESCRIPTION, ITEM_SIZE, ITEM_STATUS, RESTORE_STATUS, RECORD_STATUS, UPDATE_COUNT, CREATOR_ID, CREATE_DATE, UPDATER_ID, UPDATE_DATE) ");
        sqlStat.append("VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, nextID);
        setPrepareStatement(preStat, 2, tmpDmsVersion.getDocumentID());
        setPrepareStatement(preStat, 3, tmpDmsVersion.getVersionNumber());
        setPrepareStatement(preStat, 4, tmpDmsVersion.getVersionLabel());
        setPrepareStatement(preStat, 5, tmpDmsVersion.getParentID());
        setPrepareStatement(preStat, 6, tmpDmsVersion.getContentID());
        setPrepareStatement(preStat, 7, tmpDmsVersion.getBkArchiveID());
        setPrepareStatement(preStat, 8, tmpDmsVersion.getReferenceNo());
        setPrepareStatement(preStat, 9, tmpDmsVersion.getDescription());
        setPrepareStatement(preStat, 10, tmpDmsVersion.getItemSize());
        setPrepareStatement(preStat, 11, tmpDmsVersion.getItemStatus());
        setPrepareStatement(preStat, 12, tmpDmsVersion.getRestoreStatus());
        setPrepareStatement(preStat, 13, GlobalConstant.RECORD_STATUS_ACTIVE);
        setPrepareStatement(preStat, 14, new Integer(0));
        setPrepareStatement(preStat, 15, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 16, currTime);
        setPrepareStatement(preStat, 17, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 18, currTime);
        preStat.executeUpdate();
        tmpDmsVersion.setID(nextID);
        tmpDmsVersion.setCreatorID(sessionContainer.getUserRecordID());
        tmpDmsVersion.setCreateDate(currTime);
        tmpDmsVersion.setUpdaterID(sessionContainer.getUserRecordID());
        tmpDmsVersion.setUpdateDate(currTime);
        tmpDmsVersion.setUpdateCount(new Integer(0));
        tmpDmsVersion.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsVersion.getCreatorID()));
        tmpDmsVersion.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsVersion.getUpdaterID()));
        return(tmpDmsVersion);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_INSERT_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject update(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsVersion tmpDmsVersion = (DmsVersion)((DmsVersion)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE DMS_VERSION ");
        sqlStat.append("SET  DOCUMENT_ID=?, VERSION_NUMBER=?, VERSION_LABEL=?, PARENT_ID=?, CONTENT_ID=?, BK_ARCHIVE_ID=?, REFERENCE_NO=?, DESCRIPTION=?, ITEM_SIZE=?, ITEM_STATUS=?, RESTORE_STATUS=?, UPDATE_COUNT=?, UPDATER_ID=?, UPDATE_DATE=? ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsVersion.getDocumentID());
        setPrepareStatement(preStat, 2, tmpDmsVersion.getVersionNumber());
        setPrepareStatement(preStat, 3, tmpDmsVersion.getVersionLabel());
        setPrepareStatement(preStat, 4, tmpDmsVersion.getParentID());
        setPrepareStatement(preStat, 5, tmpDmsVersion.getContentID());
        setPrepareStatement(preStat, 6, tmpDmsVersion.getBkArchiveID());
        setPrepareStatement(preStat, 7, tmpDmsVersion.getReferenceNo());
        setPrepareStatement(preStat, 8, tmpDmsVersion.getDescription());
        setPrepareStatement(preStat, 9, tmpDmsVersion.getItemSize());
        setPrepareStatement(preStat, 10, tmpDmsVersion.getItemStatus());
        setPrepareStatement(preStat, 11, tmpDmsVersion.getRestoreStatus());
        setPrepareStatement(preStat, 12, new Integer(tmpDmsVersion.getUpdateCount().intValue() + 1));
        setPrepareStatement(preStat, 13, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 14, currTime);
        setPrepareStatement(preStat, 15, tmpDmsVersion.getID());
        setPrepareStatement(preStat, 16, tmpDmsVersion.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          tmpDmsVersion.setUpdaterID(sessionContainer.getUserRecordID());
          tmpDmsVersion.setUpdateDate(currTime);
          tmpDmsVersion.setUpdateCount(new Integer(tmpDmsVersion.getUpdateCount().intValue() + 1));
          tmpDmsVersion.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsVersion.getCreatorID()));
          tmpDmsVersion.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsVersion.getUpdaterID()));
          return(tmpDmsVersion);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized AbstractBaseObject delete(AbstractBaseObject obj) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsVersion tmpDmsVersion = (DmsVersion)((DmsVersion)obj).clone();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        sqlStat.append("DELETE ");
        sqlStat.append("FROM   DMS_VERSION ");
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, tmpDmsVersion.getID());
        setPrepareStatement(preStat, 2, tmpDmsVersion.getUpdateCount());
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        } else {
          return(tmpDmsVersion);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  
  protected synchronized void auditTrail(String opMode, AbstractBaseObject obj) throws ApplicationException {
    Vector oldValues = new Vector();
    Vector newValues = new Vector();
    DmsVersion tmpDmsVersion = (DmsVersion)this.oldValue;

    if (tmpDmsVersion != null) {
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getID()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getDocumentID()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getVersionNumber()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getVersionLabel()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getParentID()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getContentID()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getBkArchiveID()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getReferenceNo()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getDescription()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getItemSize()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getItemStatus()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getRestoreStatus()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getRecordStatus()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getUpdateCount()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getCreatorID()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getCreateDate()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getUpdaterID()));
      oldValues.add(toAuditTrailValue(tmpDmsVersion.getUpdateDate()));
    }

    tmpDmsVersion = (DmsVersion)obj;
    if (tmpDmsVersion != null) {
      newValues.add(toAuditTrailValue(tmpDmsVersion.getID()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getDocumentID()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getVersionNumber()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getVersionLabel()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getParentID()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getContentID()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getBkArchiveID()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getReferenceNo()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getDescription()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getItemSize()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getItemStatus()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getRestoreStatus()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getRecordStatus()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getUpdateCount()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getCreatorID()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getCreateDate()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getUpdaterID()));
      newValues.add(toAuditTrailValue(tmpDmsVersion.getUpdateDate()));
    }

    auditTrailBase(opMode, oldValues, newValues);
  }

  /***********************************************************************
   * DON'T Modify the codes above unless you know what you are doing!!!  *
   * Put your own functions beblow.                                      *
   * For FINDER methods, the function name should be in the notation:    *
   *   public Object getObjectBy<Criteria>()                             *
   *   - e.g. public Object getObjectByCode()                            *
   *   public List getListBy<Criteria>()                                 *
   *   - e.g. public List getListByUserID()                              *
   * For OPERATION methods, the function name should be in the notation: *
   *   public void <Operation>ObjectBy<Criteria>()                       *
   *   - e.g. public void deleteObjectByCode()                           *
   *   public void <Operation>ListBy<Criteria>()                         *
   *   - e.g. public void deleteListByUserID()                           *
   ***********************************************************************/

  /**
   * Gets the newest active version of the document by document ID
   * 
   * @param docID the dmsDocument object ID
   * @return the dmsVersion that is the newest version
   * @throws ApplicationException
   */
  public synchronized AbstractBaseObject getTopVersionByDocumentID(Integer docID) throws ApplicationException {
    return getTopVersionByDocumentID(docID, GlobalConstant.RECORD_STATUS_ACTIVE);
  }
  
  /**
   * Gets the newest version of the document by document ID
   * 
   * @param docID the dmsDocument object ID 
   * @param status the record status
   * @return abstract object
   * @throws ApplicationException
   */
  public synchronized AbstractBaseObject getTopVersionByDocumentID(Integer docID, String status) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    DmsVersion tmpDmsVersion = new DmsVersion();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_NUMBER, A.VERSION_LABEL, A.PARENT_ID, A.CONTENT_ID, A.BK_ARCHIVE_ID, A.REFERENCE_NO, A.DESCRIPTION, A.ITEM_SIZE, A.ITEM_STATUS, A.RESTORE_STATUS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_VERSION A ");
        sqlStat.append("WHERE  A.DOCUMENT_ID =? ");
        if (status!=null) {
          sqlStat.append("AND A.RECORD_STATUS = ? ");
        }
        sqlStat.append("ORDER BY  A.ID DESC  "); //get top version regardless of the major or minor
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, docID);
        if (status!=null) {
          this.setPrepareStatement(preStat, 2, status);
        }
        rs = preStat.executeQuery();
        if (rs.next()) {
          tmpDmsVersion.setID(getResultSetInteger(rs, "ID"));
          tmpDmsVersion.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsVersion.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpDmsVersion.setVersionLabel(getResultSetString(rs, "VERSION_LABEL"));
          tmpDmsVersion.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpDmsVersion.setContentID(getResultSetInteger(rs, "CONTENT_ID"));
          tmpDmsVersion.setBkArchiveID(getResultSetInteger(rs, "BK_ARCHIVE_ID"));
          tmpDmsVersion.setReferenceNo(getResultSetString(rs, "REFERENCE_NO"));
          tmpDmsVersion.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsVersion.setItemSize(getResultSetInteger(rs, "ITEM_SIZE"));
          tmpDmsVersion.setItemStatus(getResultSetString(rs, "ITEM_STATUS"));
          tmpDmsVersion.setRestoreStatus(getResultSetString(rs, "RESTORE_STATUS"));
          tmpDmsVersion.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsVersion.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsVersion.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsVersion.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsVersion.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsVersion.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsVersion.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsVersion.getCreatorID()));
          tmpDmsVersion.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsVersion.getUpdaterID()));

        }
        return(tmpDmsVersion);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets the list of the dmsVersion objects by parent ID and document ID
   * 
   * @param parentID the parent ID, if null the search by document ID only
   * @param documentID the document ID
   * @return list of the dmsVersion objects  
   * @throws ApplicationException
   */
  public synchronized List getParentVersionsListByParentIDByDocumentID(Integer parentID, Integer documentID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_NUMBER, A.VERSION_LABEL, A.PARENT_ID, A.CONTENT_ID, A.BK_ARCHIVE_ID, A.REFERENCE_NO, A.DESCRIPTION, A.ITEM_SIZE, A.ITEM_STATUS, A.RESTORE_STATUS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_VERSION A ");
        sqlStat.append("WHERE  A.DOCUMENT_ID = ? AND A.RECORD_STATUS = ? ");
        if (parentID !=null) {
          sqlStat.append(" AND A.PARENT_ID = ? ");
        }
        //sqlStat.append("ORDER BY A.PARENT_ID DESC, A.VERSION_NUMBER DESC ");
        sqlStat.append("ORDER BY A.ID DESC");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);

        this.setPrepareStatement(preStat, 1, documentID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        if (parentID !=null) {
          this.setPrepareStatement(preStat, 3, parentID);
        }
      //log.debug("sql = " +sqlStat.toString());
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsVersion tmpDmsVersion = new DmsVersion();
          tmpDmsVersion.setID(getResultSetInteger(rs, "ID"));
          tmpDmsVersion.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsVersion.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpDmsVersion.setVersionLabel(getResultSetString(rs, "VERSION_LABEL"));
          tmpDmsVersion.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpDmsVersion.setContentID(getResultSetInteger(rs, "CONTENT_ID"));
          tmpDmsVersion.setBkArchiveID(getResultSetInteger(rs, "BK_ARCHIVE_ID"));
          tmpDmsVersion.setReferenceNo(getResultSetString(rs, "REFERENCE_NO"));
          tmpDmsVersion.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsVersion.setItemSize(getResultSetInteger(rs, "ITEM_SIZE"));
          tmpDmsVersion.setItemStatus(getResultSetString(rs, "ITEM_STATUS"));
          tmpDmsVersion.setRestoreStatus(getResultSetString(rs, "RESTORE_STATUS"));
          tmpDmsVersion.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsVersion.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsVersion.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsVersion.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsVersion.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsVersion.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsVersion.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsVersion.getCreatorID()));
          tmpDmsVersion.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsVersion.getUpdaterID()));
          result.add(tmpDmsVersion);
        }
        return(result);
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
        try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Gets the version List by document ID and the query order can be specified by order parameter.
   * 
   * @param documentID the document ID
   * @param order the order, ASC - ascending , DESC- descending order
   * @return the list of the DmsVersion Object
   * @throws ApplicationException
   */
  public synchronized List getVersionsListByDocumentID(Integer documentID, String order) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_NUMBER, A.VERSION_LABEL, A.PARENT_ID, A.CONTENT_ID, A.BK_ARCHIVE_ID, A.REFERENCE_NO, A.DESCRIPTION, A.ITEM_SIZE, A.ITEM_STATUS, A.RESTORE_STATUS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_VERSION A ");
        sqlStat.append("WHERE  A.DOCUMENT_ID = ? AND A.RECORD_STATUS = ? ");
        //sqlStat.append("ORDER BY A.VERSION_NUMBER "+order);
        sqlStat.append("ORDER BY A.ID "+order);//Update By Jim Zhou     2006/12/22    EIP-1245
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, documentID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsVersion tmpDmsVersion = new DmsVersion();
          tmpDmsVersion.setID(getResultSetInteger(rs, "ID"));
          tmpDmsVersion.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsVersion.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpDmsVersion.setVersionLabel(getResultSetString(rs, "VERSION_LABEL"));
          tmpDmsVersion.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpDmsVersion.setContentID(getResultSetInteger(rs, "CONTENT_ID"));
          tmpDmsVersion.setBkArchiveID(getResultSetInteger(rs, "BK_ARCHIVE_ID"));
          tmpDmsVersion.setReferenceNo(getResultSetString(rs, "REFERENCE_NO"));
          tmpDmsVersion.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsVersion.setItemSize(getResultSetInteger(rs, "ITEM_SIZE"));
          tmpDmsVersion.setItemStatus(getResultSetString(rs, "ITEM_STATUS"));
          tmpDmsVersion.setRestoreStatus(getResultSetString(rs, "RESTORE_STATUS"));
          tmpDmsVersion.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsVersion.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsVersion.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsVersion.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsVersion.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsVersion.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsVersion.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsVersion.getCreatorID()));
          tmpDmsVersion.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsVersion.getUpdaterID()));
          result.add(tmpDmsVersion);
        }
        return(result);
      } catch (ApplicationException appEx) {
          throw appEx;
      } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }  
    }
  }

  /**
   * Gets list of the version by document ID and archive ID.
   * 
   * @param documentID the dmsDocument object ID
   * @param archiveID the dmsArchive object ID
   * @return list of the dmsVersion objects matched
   * @throws ApplicationException
   * @see com.dcivision.dms.bean.DmsArchive
   */
  public synchronized List getVersionsListByDocumentIDArchiveID(Integer documentID, Integer archiveID) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    List result = new ArrayList();

    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_NUMBER, A.VERSION_LABEL, A.PARENT_ID, A.CONTENT_ID, A.BK_ARCHIVE_ID, A.REFERENCE_NO, A.DESCRIPTION, A.ITEM_SIZE, A.ITEM_STATUS, A.RESTORE_STATUS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_VERSION A, DMS_ARCHIVE_DETAIL B ");
        sqlStat.append("WHERE  A.DOCUMENT_ID = ? AND A.DOCUMENT_ID = B.DOCUMENT_ID AND A.ID = B.VERSION_ID ");
        sqlStat.append("AND A.RECORD_STATUS = ? ");
        sqlStat.append("AND B.ARCHIVE_ID = ? ");
        sqlStat.append("ORDER BY A.VERSION_NUMBER ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, documentID);
        this.setPrepareStatement(preStat, 2, GlobalConstant.RECORD_STATUS_ACTIVE);
        this.setPrepareStatement(preStat, 3, archiveID);
        rs = preStat.executeQuery();
        while (rs.next()) {
          DmsVersion tmpDmsVersion = new DmsVersion();
          tmpDmsVersion.setID(getResultSetInteger(rs, "ID"));
          tmpDmsVersion.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsVersion.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpDmsVersion.setVersionLabel(getResultSetString(rs, "VERSION_LABEL"));
          tmpDmsVersion.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpDmsVersion.setContentID(getResultSetInteger(rs, "CONTENT_ID"));
          tmpDmsVersion.setBkArchiveID(getResultSetInteger(rs, "BK_ARCHIVE_ID"));
          tmpDmsVersion.setReferenceNo(getResultSetString(rs, "REFERENCE_NO"));
          tmpDmsVersion.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsVersion.setItemSize(getResultSetInteger(rs, "ITEM_SIZE"));
          tmpDmsVersion.setItemStatus(getResultSetString(rs, "ITEM_STATUS"));
          tmpDmsVersion.setRestoreStatus(getResultSetString(rs, "RESTORE_STATUS"));
          tmpDmsVersion.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsVersion.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsVersion.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsVersion.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsVersion.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsVersion.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsVersion.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsVersion.getCreatorID()));
          tmpDmsVersion.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsVersion.getUpdaterID()));
          result.add(tmpDmsVersion);
        }
        return(result);
      } catch (ApplicationException appEx) {
          throw appEx;
      } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Delete the table "DMS_VERSION" record by document id
   * 
   * @param dmsDocumentID the document ID
   * @throws ApplicationException
   */
  public synchronized void deleteByDocumentID(Integer dmsDocumentID) throws ApplicationException {
    PreparedStatement preStat = null;
      StringBuffer sqlStat = new StringBuffer();
      synchronized(dbConn) {
        try {
          int updateCnt = 0;
          sqlStat.append("DELETE ");
          sqlStat.append("FROM   DMS_VERSION ");
          sqlStat.append("WHERE  DOCUMENT_ID=?  ");
          preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
          setPrepareStatement(preStat, 1, dmsDocumentID);
          updateCnt = preStat.executeUpdate();
        } catch (SQLException sqle) {
            log.error(sqle, sqle);
            throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
        } catch (Exception e) {
            log.error(e, e);
            throw new ApplicationException(ErrorConstant.DB_DELETE_ERROR, e);
        } finally {
            try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
        }
    }
  }
  
  /**
   * Hard delete (delete permanently) with specified abstract dmsVersion object.
   * 
   * @param obj the abstract dmsVersion object
   * @throws ApplicationException
   */
  public synchronized void hardDeleteByID(AbstractBaseObject obj) throws ApplicationException {
    this.delete(obj);
  }
  
  /**
   * Get document item size by it's id.
   * key is document id, value 
   * Author lee.lv in 06/06/19
   * @param documentIds
   * @return Map object, key is document id, value is item size total
   * @throws ApplicationException
   */
  public synchronized Map getItemSizeTotalMap(List documentIds) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.DOCUMENT_ID, SUM(A.ITEM_SIZE) AS ITEM_SIZE ");
        sqlStat.append("FROM   DMS_VERSION A ");
        sqlStat.append("WHERE  A.DOCUMENT_ID IN (");
        for (int i=0; i<documentIds.size(); i++) {
          sqlStat.append(i!=0?",?":"?");
        }
        sqlStat.append(") ");
        sqlStat.append("GROUP BY A.DOCUMENT_ID");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int counter = 1;
        for (int i=0; i<documentIds.size(); i++) {
          this.setPrepareStatement(preStat, counter++, (Integer)documentIds.get(i));
        }
        rs = preStat.executeQuery();

        Map result = new HashMap();
        while (rs.next()) {
          result.put(getResultSetInteger(rs, "DOCUMENT_ID"), getResultSetString(rs, "ITEM_SIZE"));
        }
        return(result);
      } catch (ApplicationException appEx) {
          throw appEx;
      } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  /**
   * Get all document item size total by gived documents' id.
   * key is document id, value 
   * Author lee.lv in 06/06/19
   * @param documentIds
   * @return 
   * @throws ApplicationException
   */
  public synchronized Long getItemSizeTotal(List documentIds) throws ApplicationException {
    return this.getItemSizeTotal(documentIds, false);
  }
  
  public synchronized Long getItemSizeTotal(List documentIds, boolean isOnlyTopVersion) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT SUM(A.ITEM_SIZE) AS ITEM_SIZE ");
        sqlStat.append("FROM   DMS_VERSION A ");
        if (isOnlyTopVersion) {
          sqlStat.append("WHERE A.ID IN (SELECT MAX(ID) FROM DMS_VERSION WHERE DOCUMENT_ID IN (");
          for (int i=0; i<documentIds.size(); i++) {
            sqlStat.append(i!=0?",?":"?");
          }
          sqlStat.append(") GROUP BY DOCUMENT_ID) ");
        }else {
          sqlStat.append("WHERE  A.DOCUMENT_ID IN (");
          for (int i=0; i<documentIds.size(); i++) {
            sqlStat.append(i!=0?",?":"?");
          }
          sqlStat.append(") ");
        }
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int counter = 1;
        for (int i=0; i<documentIds.size(); i++) {
          this.setPrepareStatement(preStat, counter++, (Integer)documentIds.get(i));
        }
        rs = preStat.executeQuery();
        if (rs.next()) {
          return new Long(getResultSetString(rs, "ITEM_SIZE")!=null ? getResultSetString(rs, "ITEM_SIZE") : "0");
        }else {
          return new Long("0");
        }
      } catch (ApplicationException appEx) {
          throw appEx;
      } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized AbstractBaseObject insertOnly(AbstractBaseObject obj) throws ApplicationException {
    return this.insert(obj);
  }
  
  public synchronized AbstractBaseObject updateOnly(AbstractBaseObject obj) throws ApplicationException {
    return this.update(obj);
  }
  
  public List getTopVersionIDListByDocumentIDList(List documentIds) throws ApplicationException {
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
      StringBuffer sql = new StringBuffer();
      sql.append("SELECT MAX(ID) AS ID FROM DMS_VERSION ");
      sql.append("WHERE DOCUMENT_ID IN (");
      for (int i=0; i<documentIds.size(); i++) {
        sql.append(i!=0?",?":"?");
      }
      sql.append(") GROUP BY DOCUMENT_ID");
      pstmt = dbConn.prepareStatement(sql.toString());
      int counter = 1;
      for (int i=0; i<documentIds.size(); i++) {
        pstmt.setInt(counter++, ((Integer)documentIds.get(i)).intValue());
      }
      rs = pstmt.executeQuery();
      
      List result = new ArrayList();
      while (rs.next()) {
        result.add(new Integer(rs.getString("ID")));
      }
      return result;
    } catch (SQLException sqle) {
      log.error(sqle, sqle);
      throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
    } catch (Exception e) {
      log.error(e, e);
      throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
    } finally {
      try { rs.close();} catch (Exception ignore) {} finally {rs = null;}
      try { pstmt.close();} catch (Exception ignore) {} finally {pstmt = null;}
    }
  }
  
  public synchronized List getTopVersionListByDocumentIDList(List documentIds) throws ApplicationException {
    List topVersionIds = this.getTopVersionIDListByDocumentIDList(documentIds);
    if (topVersionIds==null || topVersionIds.isEmpty()) {
      return null;
    }
    
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_NUMBER, A.VERSION_LABEL, A.PARENT_ID, A.CONTENT_ID, A.BK_ARCHIVE_ID, A.REFERENCE_NO, A.DESCRIPTION, A.ITEM_SIZE, A.ITEM_STATUS, A.RESTORE_STATUS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_VERSION A ");
        sqlStat.append("WHERE  A.ID IN (");
        for (int i=0; i<topVersionIds.size(); i++) {
          sqlStat.append(i!=0?",?":"?");
        }
        sqlStat.append(") ");
        sqlStat.append("ORDER BY A.DOCUMENT_ID, A.ID");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int counter = 1;
        for (int i=0; i<topVersionIds.size(); i++) {
          this.setPrepareStatement(preStat, counter++, (Integer)topVersionIds.get(i));
        }
        rs = preStat.executeQuery();

        List result = new ArrayList();
        while (rs.next()) {
          DmsVersion tmpDmsVersion = new DmsVersion();
          tmpDmsVersion.setID(getResultSetInteger(rs, "ID"));
          tmpDmsVersion.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsVersion.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpDmsVersion.setVersionLabel(getResultSetString(rs, "VERSION_LABEL"));
          tmpDmsVersion.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpDmsVersion.setContentID(getResultSetInteger(rs, "CONTENT_ID"));
          tmpDmsVersion.setBkArchiveID(getResultSetInteger(rs, "BK_ARCHIVE_ID"));
          tmpDmsVersion.setReferenceNo(getResultSetString(rs, "REFERENCE_NO"));
          tmpDmsVersion.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsVersion.setItemSize(getResultSetInteger(rs, "ITEM_SIZE"));
          tmpDmsVersion.setItemStatus(getResultSetString(rs, "ITEM_STATUS"));
          tmpDmsVersion.setRestoreStatus(getResultSetString(rs, "RESTORE_STATUS"));
          tmpDmsVersion.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsVersion.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsVersion.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsVersion.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsVersion.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsVersion.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsVersion.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsVersion.getCreatorID()));
          tmpDmsVersion.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsVersion.getUpdaterID()));
          result.add(tmpDmsVersion);
        }
        return(result);
      } catch (ApplicationException appEx) {
          throw appEx;
      } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized List getVersionListByDocumentIDList(List documentIds) throws ApplicationException {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID, A.DOCUMENT_ID, A.VERSION_NUMBER, A.VERSION_LABEL, A.PARENT_ID, A.CONTENT_ID, A.BK_ARCHIVE_ID, A.REFERENCE_NO, A.DESCRIPTION, A.ITEM_SIZE, A.ITEM_STATUS, A.RESTORE_STATUS, A.RECORD_STATUS, A.UPDATE_COUNT, A.CREATOR_ID, A.CREATE_DATE, A.UPDATER_ID, A.UPDATE_DATE ");
        sqlStat.append("FROM   DMS_VERSION A ");
        sqlStat.append("WHERE  A.DOCUMENT_ID IN (");
        for (int i=0; i<documentIds.size(); i++) {
          sqlStat.append(i!=0?",?":"?");
        }
        sqlStat.append(") ");
        sqlStat.append("ORDER BY A.DOCUMENT_ID, A.ID");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        int counter = 1;
        for (int i=0; i<documentIds.size(); i++) {
          this.setPrepareStatement(preStat, counter++, (Integer)documentIds.get(i));
        }
        rs = preStat.executeQuery();

        List result = new ArrayList();
        while (rs.next()) {
          DmsVersion tmpDmsVersion = new DmsVersion();
          tmpDmsVersion.setID(getResultSetInteger(rs, "ID"));
          tmpDmsVersion.setDocumentID(getResultSetInteger(rs, "DOCUMENT_ID"));
          tmpDmsVersion.setVersionNumber(getResultSetString(rs, "VERSION_NUMBER"));
          tmpDmsVersion.setVersionLabel(getResultSetString(rs, "VERSION_LABEL"));
          tmpDmsVersion.setParentID(getResultSetInteger(rs, "PARENT_ID"));
          tmpDmsVersion.setContentID(getResultSetInteger(rs, "CONTENT_ID"));
          tmpDmsVersion.setBkArchiveID(getResultSetInteger(rs, "BK_ARCHIVE_ID"));
          tmpDmsVersion.setReferenceNo(getResultSetString(rs, "REFERENCE_NO"));
          tmpDmsVersion.setDescription(getResultSetString(rs, "DESCRIPTION"));
          tmpDmsVersion.setItemSize(getResultSetInteger(rs, "ITEM_SIZE"));
          tmpDmsVersion.setItemStatus(getResultSetString(rs, "ITEM_STATUS"));
          tmpDmsVersion.setRestoreStatus(getResultSetString(rs, "RESTORE_STATUS"));
          tmpDmsVersion.setRecordStatus(getResultSetString(rs, "RECORD_STATUS"));
          tmpDmsVersion.setUpdateCount(getResultSetInteger(rs, "UPDATE_COUNT"));
          tmpDmsVersion.setCreatorID(getResultSetInteger(rs, "CREATOR_ID"));
          tmpDmsVersion.setCreateDate(getResultSetTimestamp(rs, "CREATE_DATE"));
          tmpDmsVersion.setUpdaterID(getResultSetInteger(rs, "UPDATER_ID"));
          tmpDmsVersion.setUpdateDate(getResultSetTimestamp(rs, "UPDATE_DATE"));
          tmpDmsVersion.setCreatorName(UserInfoFactory.getUserFullName(tmpDmsVersion.getCreatorID()));
          tmpDmsVersion.setUpdaterName(UserInfoFactory.getUserFullName(tmpDmsVersion.getUpdaterID()));
          result.add(tmpDmsVersion);
        }
        return(result);
      } catch (ApplicationException appEx) {
          throw appEx;
      } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }
  
  public synchronized void updateByFields(
      String[] fields, 
      Object[] values,
      Integer id,
      Integer updateCount) throws ApplicationException {
    PreparedStatement preStat = null;
    StringBuffer sqlStat = new StringBuffer();

    synchronized(dbConn) {
      try {
        int updateCnt = 0;
        Timestamp currTime = Utility.getCurrentTimestamp();
        sqlStat.append("UPDATE DMS_VERSION ");
        sqlStat.append("SET UPDATE_COUNT=UPDATE_COUNT+1, UPDATER_ID=?, UPDATE_DATE=? ");
        if (fields!=null) {
          for (int i=0; i<fields.length; i++) {
            sqlStat.append("," +fields[i]+ "=?  ");
          }
        }
        sqlStat.append("WHERE  ID=? AND UPDATE_COUNT=? ");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        setPrepareStatement(preStat, 1, sessionContainer.getUserRecordID());
        setPrepareStatement(preStat, 2, currTime);
        int counter = 3;
        if (fields!=null) {
          for (int i=0; i<values.length; i++) {
            if (values[i]==null) {
              preStat.setObject(counter++, null);
            }else {
              String className = values[i].getClass().getName();
              if (className.indexOf("Integer")!=-1) {
                setPrepareStatement(preStat, counter++, (Integer)values[i]);
              }else if (className.indexOf("String")!=-1) {
                setPrepareStatement(preStat, counter++, (String)values[i]);
              }else if (className.indexOf("Timestamp")!=-1) {
                setPrepareStatement(preStat, counter++, (Timestamp)values[i]);
              }else {
                throw new ApplicationException("DmsVersionDAObject.updateFields() does not support this field type:"+className);
              }
            }
          }
        }
        setPrepareStatement(preStat, counter++, id);
        setPrepareStatement(preStat, counter++, updateCount);
        updateCnt = preStat.executeUpdate();
        if (updateCnt == 0) {
          throw new ApplicationException(ErrorConstant.DB_CONCURRENT_ERROR);
        }
      } catch (ApplicationException appEx) {
        throw appEx;
      } catch (SQLException sqle) {
        log.error(sqle, sqle);
        throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
        log.error(e, e);
        throw new ApplicationException(ErrorConstant.DB_UPDATE_ERROR, e);
      } finally {
        try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
  }

  /**
   * get the dmsVersion ID list which existed in db.(EIP-2241).
   * @param workflowProgressAttachmentList the list of workflowProgressAttachment.
   * @return the dmsVersion ID list which existed in db.
   * @throws ApplicationException ApplicationException.
   */
  public synchronized List getExistDmsVersionIDByList(
        List workflowProgressAttachmentList) throws ApplicationException {
  {
    PreparedStatement preStat = null;
    ResultSet rs = null;
    StringBuffer sqlStat = new StringBuffer();
    synchronized(dbConn) {
      try {
        sqlStat.append("SELECT A.ID ");
        sqlStat.append("FROM DMS_VERSION A ");
        sqlStat.append("WHERE  A.RECORD_STATUS = ? AND A.ID IN (");
        for (int i = 0; i < workflowProgressAttachmentList.size(); i++) {
          sqlStat.append(i!=0?",?":"?");
        }
        sqlStat.append(") ");
        sqlStat.append("ORDER BY A.ID");
        preStat = dbConn.prepareStatement(sqlStat.toString(), ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        this.setPrepareStatement(preStat, 1, GlobalConstant.RECORD_STATUS_ACTIVE);
        int counter = 2;
        for (int i=0; i < workflowProgressAttachmentList.size(); i++) {
          WorkflowProgressAttachment tempWfProgressAttachment = (WorkflowProgressAttachment)
               workflowProgressAttachmentList.get(i);
          this.setPrepareStatement(preStat, counter++, tempWfProgressAttachment.getExternalID());
        }
        rs = preStat.executeQuery();

        List result = new ArrayList();
        while (rs.next()) {
          DmsVersion tmpDmsVersion = new DmsVersion();
          tmpDmsVersion.setID(getResultSetInteger(rs, "ID"));
          result.add(tmpDmsVersion);
        }
        return(result);
      } catch (ApplicationException appEx) {
          throw appEx;
      } catch (SQLException sqle) {
          log.error(sqle, sqle);
          throw new ApplicationException(ErrorConstant.DB_GENERAL_ERROR, sqle, sqle.toString());
      } catch (Exception e) {
          log.error(e, e);
          throw new ApplicationException(ErrorConstant.DB_SELECT_ERROR, e);
      } finally {
          try { rs.close(); } catch (Exception ignore) {} finally { rs = null; }
          try { preStat.close(); } catch (Exception ignore) {} finally { preStat = null; }
      }
    }
   }
  }
}
